<?php

namespace app\crm\controller\auth;

use app\crm\controller\BaseController;

use app\crm\model\auth\AuthNode;
use app\crm\model\auth\Role;
use app\crm\model\auth\Admin as AuthAdmin;

class Admin extends BaseController
{
    
    public function index()
    {
        $admin = AuthAdmin::all();
        if ($this->request->isAjax()) {
            $data = $this->request->get();
            if (count($data) >= 7) {
                $appoint = AuthAdmin::WhereLike('id', $data['id'] . '%') // 搜索
                    ->WhereLike('account', $data['account'] . '%')
                    ->WhereLike('admin', $data['admin'] . '%')
                    ->WhereLike('role_id', $data['role_id'] . '%')
                    ->WhereLike('phone', $data['phone'] . '%')
                    ->WhereLike('status', $data['status'] . '%')
                    ->WhereLike('update_time', $data['update_time'] . '%')
                    ->all();
                return json(['code' => 0, 'mag' => '分页', 'count' => count($appoint), 'data' => $appoint]);
            } else {
                $admin = AuthAdmin::all();
                foreach ($admin as $val) {
                    !empty($val->role['auth_name']) && $val['rolex'] = $val->role['auth_name'];
                }
                return json(['code' => 0, 'mag' => '分页', 'count' => count(AuthAdmin::all()), 'data' => $admin]);
            }
        }

        return $this->fetch('', [
            'role' => Role::all()
        ]);
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            isset($post['role_id']) && $post['role_id'] = join(',', $post['role_id']);
            !isset($post['role_id']) && $post['role_id'] = '';
            $rule = [
                'phone|手机号' => 'mobile'
            ];
            $result = $this->validate($post, $rule);
            $result !== true && $this->error($result);
            try {
                $save = (new AuthAdmin())->save($post);
            } catch (\Exception $e) {
                $this->error('添加失败');
            }
            $save ? $this->success('添加成功') : $this->error('添加失败');
        }

        return $this->fetch('', [
            'role' => Role::all()
        ]);
    }

    public function edit($account = '')
    {
        $row = AuthAdmin::where('account',$account)->find();
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            // 修改状态
            isset($post['status']) &&  $row->save($post) == true && $this->success('修改成功'); // 修改状态
            isset($post['role_id']) && $post['role_id'] = join(',', $post['role_id']);
            !isset($post['role_id']) && $post['role_id'] = '';
            $rule = [
                'phone|手机号' => 'mobile'
            ];
            $result = $this->validate($post, $rule);
            $result !== true && $this->error($result);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('修改失败');
            }
            $save ? $this->success('修改成功') : $this->error('修改失败');
        }

        return $this->fetch('add', [
            'role' => Role::all(),
            'row'  => $row,
            'edit' => 'edit'
        ]);
    }

    public function delete($id = '')
    {
        $row = AuthAdmin::get($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            AuthAdmin::destroy($id);
            $this->success('删除成功');
        }
        return $this->fetch();
    }
}
